


//将表单序列化为JSON
$.fn.serializeObject = function(){    
   var o = {};    
   var a = this.serializeArray();    
   $.each(a, function() {    
       if (o[this.name]) {    
           if (!o[this.name].push) {    
               o[this.name] = [o[this.name]];    
           }    
           o[this.name].push(this.value || '');    
       } else {    
           o[this.name] = this.value || '';    
       }    
   });    
   return o;    
};  
//毫秒转换成置顶日期格式
//调用格式为  date = format(Date,yyyy-MM-dd)
var format = function(time, format){
    var t = new Date(time);
    var tf = function(i){return (i < 10 ? '0' : '') + i};
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
        switch(a){
            case 'yyyy':
                return tf(t.getFullYear());
                break;
            case 'MM':
                return tf(t.getMonth() + 1);
                break;
            case 'mm':
                return tf(t.getMinutes());
                break;
            case 'dd':
                return tf(t.getDate());
                break;
            case 'HH':
                return tf(t.getHours());
                break;
            case 'ss':
                return tf(t.getSeconds());
                break;
        }
    })
}

// dataTables 引用
/***
 * contId:dataTables 的ID
 * url：请求的链接
 * listArr: 对象拼接
 * 
 */
function matchTable(contId, url, listArr, funParam, destorys) {
	if(destorys){
		if($.fn.dataTable.isDataTable(contId)){
	        _table = $(contId).DataTable();
	        _table.destroy();
		}
	}
    //初始化表格
    var table = $(contId).DataTable(
            {
//            	"scrollX": true,
            	"bServerSide": true,
                "ordering": false,
                "searching": false,
                "LengthChange": true,
                "bLengthChange": true,
                "pagingType": "full_numbers",
                language: {
                    "sProcessing": "处理中...",
                    "sLengthMenu": "显示 _MENU_ 项结果",
                    "sZeroRecords": "没有匹配结果",
                    "bPaginate": true,
                    "bFilter": true,
                    "sInfo": "显示第  _START_ 至  _END_ 项结果，共  _TOTAL_ 项，当前位置  第_PAGE_页",
                    "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
//                    "sInfoFiltered": "(由  _MAX_ 项结果过滤)",
                    "sInfoFiltered": "",
                    "sInfoPostFix": "",
                    "sSearch": "搜索:",
                    "sUrl": "",
                    "sEmptyTable": "表中数据为空",
                    "sLoadingRecords": "载入中...",
                    "sInfoThousands": ",",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上页",
                        "sNext": "下页",
                        "sLast": "末页"
                    }
                },
                "bDeferRender": false,
                "retrieve": true,
                "processing": true,
                "ajax": {
                    url: webroot + url,
                    type: "POST",
                    data: funParam
                },
                "columns": listArr
            });
    $(contId).wrap('<div style="width:100%;overflow-x:auto;"></div>');
    
    return table;
}


function matchNotPageTable(contId, url, listArr, funParam, destorys) {
	if(destorys){
		if($.fn.dataTable.isDataTable(contId)){
	        _table = $(contId).DataTable();
	        _table.destroy();
		}
	}
    //初始化表格
    var table = $(contId).DataTable(
            {
//            	"scrollX": true,
            	"bServerSide": true,
                "ordering": false,
                "searching": false,
                "bLengthChange": true,
                "paging": false,
                language: {
                    "sProcessing": "处理中...",
                    "sLengthMenu": "显示 _MENU_ 项结果",
                    "sZeroRecords": "没有匹配结果",
                    "bPaginate": true,
                    "bFilter": true,
                    "sInfo": "显示第  _START_ 至  _END_ 项结果，共  _TOTAL_ 项，当前位置  第_PAGE_页",
                    "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
//                    "sInfoFiltered": "(由  _MAX_ 项结果过滤)",
                    "sInfoFiltered": "",
                    "sInfoPostFix": "",
                    "sSearch": "搜索:",
                    "sUrl": "",
                    "sEmptyTable": "表中数据为空",
                    "sLoadingRecords": "载入中...",
                    "sInfoThousands": ",",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上页",
                        "sNext": "下页",
                        "sLast": "末页"
                    }
                },
                "bDeferRender": false,
                "retrieve": true,
                "processing": true,
                "ajax": {
                    url: webroot + url,
                    type: "POST",
                    data: funParam
                },
                "columns": listArr
            });
    $(contId).wrap('<div style="width:100%;overflow-x:auto;"></div>');
    
    return table;
}



function notPageTableByArrayData(contId, arrayData, listArr, destorys) {
	if(destorys){
		if($.fn.dataTable.isDataTable(contId)){
	        _table = $(contId).DataTable();
	        _table.destroy();
		}
	}
    //初始化表格
    var table = $(contId).DataTable(
            {
            	"bServerSide": false,//服务器端分页,数据源为javascript数组的话必须为false
                "ordering": false,
                "searching": false,
                "bLengthChange": true,
                "paging": false,
                language: {
                    "sProcessing": "处理中...",
                    "sLengthMenu": "显示 _MENU_ 项结果",
                    "sZeroRecords": "没有匹配结果",
                    "bPaginate": true,
                    "bFilter": true,
                    "sInfo": "显示第  _START_ 至  _END_ 项结果，共  _TOTAL_ 项，当前位置  第_PAGE_页",
                    "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
//                    "sInfoFiltered": "(由  _MAX_ 项结果过滤)",
                    "sInfoFiltered": "",
                    "sInfoPostFix": "",
                    "sSearch": "搜索:",
                    "sUrl": "",
                    "sEmptyTable": "表中数据为空",
                    "sLoadingRecords": "载入中...",
                    "sInfoThousands": ",",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上页",
                        "sNext": "下页",
                        "sLast": "末页"
                    }
                },
                "bDeferRender": false,
                "retrieve": true,
                "processing": true,
                "data": arrayData,
                "columns": listArr
            });
    $(contId).wrap('<div style="width:100%;overflow-x:auto;"></div>');
    
    return table;
}

//table样式



//返回行数据 为object对象
//tableObj datatables对象 trObj tr对象
function getValueToTableByKey(tableObj, trObj){
	return tableObj.row(trObj).data();
}
//点击图片查看大图
function openImg(url){
	window.open(url,"bigImg");
}



//a标签禁用
function noClick(eve,index){
	if(arguments.length === 1){
		index = 0;
	}
	var btn = $(eve).find(".layui-layer-btn").children("a");
	$(btn[index]).css('pointer-events','none')
	             .css('opacity','0.2');
}
//a标签启用
function hasClick(eve,index){
	if(arguments.length === 1){
		index = 0;
	}
	var btn = $(eve).find(".layui-layer-btn").children("a");
	$(btn[index]).css('pointer-events','auto')
	             .css('opacity','1');
}



//显示多余文字
function displayText(eve){
	$(eve).append('<div class="display_text">'+$(eve).text()+'</div>');
	var top=$(eve).offset().top-$('.display_text').height()-$('#datatables').offset().top+50;
	var left=$(eve).offset().left-$('.display_text').width();
	top = top < 0 ? 0 : top;
	left = left < 0 ? 0 : left;
	$('.display_text').css('top',top)
	                  .css('left',left);
}

function noDisplay(eve){	
	$(eve).find('.display_text').remove();	
}


function getSelectInvestmentType(id){
	
	$(id).select2({
        ajax: {
            url: webroot+ "/dictionary/getSelectInvestmentType.do",
            type:'POST',
            data: function (params) {		                	
                params.rows=10;
                return {
                	searchName:params.term || '', 
                	currentPage:(params.page||1)-1,
                	pageSize:params.rows
                };
            },
            processResults: function (data, params) {	
            	
            	var processData = JSON.parse(data);
            	params.page = params.page || 1;
            	var selectArr = [];
            	var newData=processData.data;
	    		for(var i=0; i<newData.length; i++){
	    			selectArr.push({'id':newData[i].id,'text':newData[i].typeName})
	    		}
                return {
                    results: selectArr,
                    pagination: {	
                        more: (params.page * params.rows) < processData.count
                    }
                };
            }
        },		
        templateResult: function (state){
            if (!state.id) { return state.text; }
            var $state = $(
                '<p>' + state.text + '</p>'
            );
            return $state;
        }
    })
}

function getSelectCustType(id){
	
	$(id).select2({
        ajax: {
            url: webroot+ "/dictionary/getSelectCustType.do",
            type:'POST',
            data: function (params) {		                	
                params.rows=10;
                return {
                	searchName:params.term || '', 
                	currentPage:(params.page||1)-1,
                	pageSize:params.rows
                };
            },
            processResults: function (data, params) {	
            	
            	var processData = JSON.parse(data);
            	params.page = params.page || 1;
            	var selectArr = [];
            	var newData=processData.data;
	    		for(var i=0; i<newData.length; i++){
	    			selectArr.push({'id':newData[i].id,'text':newData[i].typeName})
	    		}
                return {
                    results: selectArr,
                    pagination: {	
                        more: (params.page * params.rows) < processData.count
                    }
                };
            }
        },		
        templateResult: function (state){
            if (!state.id) { return state.text; }
            var $state = $(
                '<p>' + state.text + '</p>'
            );
            return $state;
        }
    })
}

function getSelectMemberInfo(id){
	
	$(id).select2({
        ajax: {
            url: webroot+ "/member/getSelectMember.do",
            type:'POST',
            data: function (params) {		                	
                params.rows=10;
                return {
                	searchName:params.term || '', 
                	currentPage:(params.page||1)-1,
                	pageSize:params.rows
                };
            },
            processResults: function (data, params) {	
            	
            	var processData = JSON.parse(data);
            	params.page = params.page || 1;
            	var selectArr = [];
            	var newData=processData.data;
	    		for(var i=0; i<newData.length; i++){
	    			selectArr.push({'id':newData[i].id,'text':newData[i].name})
	    		}
                return {
                    results: selectArr,
                    pagination: {	
                        more: (params.page * params.rows) < processData.count
                    }
                };
            }
        },		
        templateResult: function (state){
            if (!state.id) { return state.text; }
            var $state = $(
                '<p>' + state.text + '</p>'
            );
            return $state;
        }
    })
}

function getSelectMemberService(id){
	
	$(id).select2({
        ajax: {
            url: webroot+ "/serviceInfo/getSelectMemberService.do",
            type:'POST',
            data: function (params) {		                	
                params.rows=10;
                return {
                	searchName:params.term || '', 
                	currentPage:(params.page||1)-1,
                	pageSize:params.rows
                };
            },
            processResults: function (data, params) {	
            	
            	var processData = JSON.parse(data);
            	params.page = params.page || 1;
            	var selectArr = [];
            	var newData=processData.data;
	    		for(var i=0; i<newData.length; i++){
	    			selectArr.push({'id':newData[i].id,'text':newData[i].serviceName})
	    		}
                return {
                    results: selectArr,
                    pagination: {	
                        more: (params.page * params.rows) < processData.count
                    }
                };
            }
        },		
        templateResult: function (state){
            if (!state.id) { return state.text; }
            var $state = $(
                '<p>' + state.text + '</p>'
            );
            return $state;
        }
    })
}


function getSelectProjectService(id){
	
	$(id).select2({
        ajax: {
            url: webroot+ "/serviceInfo/getSelectProjectService.do",
            type:'POST',
            data: function (params) {		                	
                params.rows=10;
                return {
                	searchName:params.term || '', 
                	currentPage:(params.page||1)-1,
                	pageSize:params.rows
                };
            },
            processResults: function (data, params) {	
            	
            	var processData = JSON.parse(data);
            	params.page = params.page || 1;
            	var selectArr = [];
            	var newData=processData.data;
	    		for(var i=0; i<newData.length; i++){
	    			selectArr.push({'id':newData[i].id,'text':newData[i].serviceName})
	    		}
                return {
                    results: selectArr,
                    pagination: {	
                        more: (params.page * params.rows) < processData.count
                    }
                };
            }
        },		
        templateResult: function (state){
            if (!state.id) { return state.text; }
            var $state = $(
                '<p>' + state.text + '</p>'
            );
            return $state;
        }
    })
    
}


function getCheckBoxServiceInfo(id){

	$(id).select2({
		multiple:true,
		closeOnSelect:false,
        ajax: {
            url: webroot+ "/serviceInfo/getSelectServiceInfo.do",
            type:'POST',
            data: function (params) {		                	
                params.rows=10;
                return {
                	searchName:params.term || '', 
                	currentPage:(params.page||1)-1,
                	pageSize:params.rows
                };
            },
            processResults: function (data, params) {	
            	
            	var processData = JSON.parse(data);
            	params.page = params.page || 1;
            	var selectArr = [];
            	var newData=processData.data;
	    		for(var i=0; i<newData.length; i++){
	    			selectArr.push({'id':newData[i].id,'text':newData[i].serviceName})
	    		}
                return {
                    results: selectArr,
                    pagination: {	
                        more: (params.page * params.rows) < processData.count
                    }
                };
            }
        },		
        templateResult: function (state){
            if (!state.id) { return state.text; }
            var $state = $(
                '<p>' + state.text + '</p>'
            );
            return $state;
        }
    })

}

function getCheckBoxCustType(id){

	$(id).select2({
		multiple:true,
		closeOnSelect:false,
        ajax: {
            url: webroot+ "/dictionary/getSelectCustType.do",
            type:'POST',
            data: function (params) {		                	
                params.rows=10;
                return {
                	searchName:params.term || '', 
                	currentPage:(params.page||1)-1,
                	pageSize:params.rows
                };
            },
            processResults: function (data, params) {	
            	
            	var processData = JSON.parse(data);
            	params.page = params.page || 1;
            	var selectArr = [];
            	var newData=processData.data;
	    		for(var i=0; i<newData.length; i++){
	    			selectArr.push({'id':newData[i].id,'text':newData[i].typeName})
	    		}
                return {
                    results: selectArr,
                    pagination: {	
                        more: (params.page * params.rows) < processData.count
                    }
                };
            }
        },		
        templateResult: function (state){
            if (!state.id) { return state.text; }
            var $state = $(
                '<p>' + state.text + '</p>'
            );
            return $state;
        }
    })

}

function getSelectLectrueType(id){
	
	$(id).select2({
        ajax: {
            url: webroot+ "/dictionary/getSelectLectrueType.do",
            type:'POST',
            data: function (params) {		                	
                params.rows=10;
                return {
                	searchName:params.term || '', 
                	currentPage:(params.page||1)-1,
                	pageSize:params.rows
                };
            },
            processResults: function (data, params) {	
            	
            	var processData = JSON.parse(data);
            	params.page = params.page || 1;
            	var selectArr = [];
            	var newData=processData.data;
	    		for(var i=0; i<newData.length; i++){
	    			selectArr.push({'id':newData[i].id,'text':newData[i].typeName})
	    		}
                return {
                    results: selectArr,
                    pagination: {	
                        more: (params.page * params.rows) < processData.count
                    }
                };
            }
        },		
        templateResult: function (state){
            if (!state.id) { return state.text; }
            var $state = $(
                '<p>' + state.text + '</p>'
            );
            return $state;
        }
    })
}
